package com.wcy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.annotation.aop.PreAuth;
import com.wcy.entity.LearningResource;
import com.wcy.entity.ResourceTag;
import com.wcy.entity.queryVo.QueryConvertor;
import com.wcy.service.IResourceTagService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴传亿
 * @since 2025-04-27
 */
@RestController
@RequestMapping("/resourceTag")
public class ResourceTagController {
    @Autowired
    private IResourceTagService resourceTagService;

    @PreAuth("TAG:QUERY")
    @GetMapping("/page")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<ResourceTag>> getPage( @RequestParam(defaultValue = "1") int pageNum,
                                                     @RequestParam(defaultValue = "10") int pageSize,
                                                      HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        return ResponseEntity.ok(resourceTagService.page(Page.of(pageNum, pageSize),wrapper));
    }

    @PreAuth("AUTHENTICATION")
    @GetMapping("/list")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<List<ResourceTag>> getList(HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        return ResponseEntity.ok(resourceTagService.list(wrapper));
    }

    @PreAuth("TAG:ADD")
    @PostMapping("/add")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> add(@RequestBody ResourceTag resourceTag) {
        return resourceTagService.save(resourceTag) ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    @PreAuth("TAG:ALTER")
    @PutMapping("/update")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> update(@RequestBody ResourceTag resourceTag) {
        return resourceTagService.updateById(resourceTag) ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    @PreAuth("TAG:DELETE")
    @DeleteMapping("/delete/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        return resourceTagService.removeById(id) ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    @GetMapping("/getByResourceId/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<List<ResourceTag>> getByResourceId(@PathVariable Integer id) {
        return ResponseEntity.ok(resourceTagService.getTagsByResourceId(id));
    }
}
